﻿using JT808Protocol.Common;
using System;
using System.Collections;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace JT808Protocol.Models.JsonConverters
{
    public class JT808DefaultConverter : JsonConverter<JT808Package>
    {
        public override JT808Package Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JT808Package result = new JT808Package();
            while (reader.Read())
            {
               
            }
            return result;
        }

        public override void Write(Utf8JsonWriter writer, JT808Package value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            WriteClassPropertiesToJson(writer, value.GetType(), value);
            writer.WriteEndObject();
        }

        /// <summary>
        /// 写入类属性到json字符串中
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="classType"></param>
        /// <param name="value"></param>
        private void WriteClassPropertiesToJson(Utf8JsonWriter writer, Type classType, object value)
        {
            var properties = classType.GetProperties();
            foreach (var prop in properties)
            {
                //是公共属性
                if (prop.PropertyType.IsPublic && value != null)
                {
                    //遇到空值直接跳过
                    if (prop.GetValue(value) == null)
                    {
                        continue;
                    }

                    if (!prop.PropertyType.IsSimpleType())
                    {
                        var propertyName = prop.Name;
                        writer.WritePropertyName(propertyName);

                        //是集合属性
                        if (prop.PropertyType.IsCollection())
                        {
                            writer.WriteStartArray();

                            var Items = (IList)prop.GetValue(value);
                            foreach (var item in Items)
                            {
                                //作为类写入json
                                writer.WriteStartObject();
                                WriteClassPropertiesToJson(writer, item.GetType(), item);
                                writer.WriteEndObject();
                            }

                            writer.WriteEndArray();
                        }
                        else
                        {
                            var propertyValue = prop.GetValue(value);

                            if (propertyValue != null)
                            {
                                //作为类写入json
                                writer.WriteStartObject();
                                WriteClassPropertiesToJson(writer, propertyValue.GetType(), propertyValue);
                                writer.WriteEndObject();
                            }
                        }
                    }
                    else
                    {
                        string propertyName = prop.Name;
                        string propertyValue = prop.GetValue(value) != null ? prop.GetValue(value).ToString() : "";

                        writer.WriteString(propertyName, propertyValue);
                    }
                }
            }
        }
    }
}